import 'dart:ui' as ui;

import 'package:flutter_kit_plus/flutter_kit.dart';

import 'en.g.dart';
import 'my_localizations.dart';
import 'zh.g.dart';

class I19n {
  static String? _cachedLangCode;
  static Future<void> init() async {
    await get();
  }

  static MyLocalizations get i {
    final langCode = _cachedLangCode ?? ui.PlatformDispatcher.instance.locale.languageCode.toLowerCase();
    switch (langCode) {
      case 'en':
        return MyTranslationsEn();
      case 'zh':
        return MyTranslationsZh();
      default: // 默认中文
        save('zh');
        return MyTranslationsZh();
    }
  }

  static Future<void> save(String lang) async {
    _cachedLangCode = lang;
    final prefs = await SharedPreferences.getInstance();
    prefs.setString('language', lang);
  }

  static Future<String?> get() async {
    final prefs = await SharedPreferences.getInstance();
    _cachedLangCode = prefs.getString('language');
    return _cachedLangCode;
  }

  static String getLocalLangCode() => _cachedLangCode ?? '';
}
